<?php
class StorageHandler{
	protected $StorageType;
	protected $path;
	protected $StorageObject;

	function __construct($options=null) {
		$this->StorageType=$options['StorageType'];
		switch($this->StorageType){
			case('ACE'):
				$this->path="";
				$this->StorageObject=new CEStorage();
				break;
			case('SAE'):
				$this->path=$options['domain'];
				$this->StorageObject=new SaeStorage();
				break;
			default:
				$this->StorageType='FileSystem';
				$this->StorageObject=null;		
		}
	}
	function __destruct() {
		unset( $this->StorageType);
		unset( $this->path);
		unset( $this->StorageObject);
	}

	protected function FileSystemList() {
		$val=array();
		$i=0;
		
		$d = dir($this->path);
		while (false !== ($entry = $d->read()) ) {
		   $val[$i++]= $entry ;
		}
		$d->close();
		return $val;
	}

	/*
	string upload (string $srcFileName, string $destFileName)
	  上传文件，保存至存储服务器。
	  参数：
	  srcFileName
		 需上传的文件名
	  destFileName
		 保存文件名
	  返回值：
		 成功则返回文件访问地址，否则false。
		 
	string upload (string $domain, string $destFileName, string $srcFileName, [array $attr = array()], [bool $compress = false])
	*/
	public function upload( $srcFileName , $destFileName ) {
		switch($this->StorageType){
			case('ACE'):
				return array(	'result'=> $this->StorageObject->upload( $srcFileName, $destFileName ) ,
								'errno'=> $this->StorageObject->errnum(),
								'errmsg'=> $this->StorageObject->errmsg()
							);
				break;
			case('SAE'):
				return array(	'result'=> $this->StorageObject->upload($this->path, $destFileName,$srcFileName),
								'errno'=> $this->StorageObject->errno (),
								'errmsg'=> $this->StorageObject->errmsg ()
							);
				break;
			case('FileSystem'):
				$fullname=$this->path ."/".  $destFileName ;
				$result = move_uploaded_file( $srcFileName , $fullname);
				return array(	'result'=>  $result ? $fullname : false ,
								'errno'=> $result ? 0 : 9910 ,
								'errmsg'=> $result ? "" : "unknow error" 
							);
				break;
			default:
				return array(	'result'=>  false ,
								'errno'=> 9999 ,
								'errmsg'=> "unknow type" 
							);
		}
	}

	/*
	string read (string $filename)
	  读取文件内容。
	  参数：
	  filename 
		读取的文件名
	  返回值：
		 成功返回文件内容，否则false。
		 
	string read (string $domain, string $filename)
	*/
	public function read( $filename ) {
		switch($this->StorageType){
			case('ACE'):
				return array(	'result'=> $this->StorageObject->read( $filename ) ,
								'errno'=> $this->StorageObject->errnum(),
								'errmsg'=> $this->StorageObject->errmsg()
							);
				break;
			case('SAE'):
				return array(	'result'=> $this->StorageObject->read($this->path, $path ."/".  $filename ),
								'errno'=> $this->StorageObject->errno (),
								'errmsg'=> $this->StorageObject->errmsg ()
							);
				break;
			case('FileSystem'):
				$fullname=$this->path ."/".  $filename ;
				$result = file_get_contents ( $fullname);
				return array(	'result'=>  $result ? $result : false ,
								'errno'=> $result ? 0 : 9920 ,
								'errmsg'=> $result ? "" : "unknow error" 
							);
				break;
			default:
				return array(	'result'=>  false ,
								'errno'=> 9999 ,
								'errmsg'=> "unknow type" 
							);
		}
	}

	/*
	string getUrl (string $filename)
	  获取访问文件的url。
	  参数：
	  filename
		 需获取的文件名
	  返回值：
		 成功则返回URL，否则false。
			 
	string getUrl (string $domain, string $filename)
	*/
	public function getUrl( $filename ) {
		switch($this->StorageType){
			case('ACE'):
				return array(	'result'=> $this->StorageObject->getUrl( $filename ) ,
								'errno'=> $this->StorageObject->errnum(),
								'errmsg'=> $this->StorageObject->errmsg()
							);
				break;
			case('SAE'):
				return array(	'result'=> $this->StorageObject->getUrl($this->path, $path ."/".  $filename ),
								'errno'=> $this->StorageObject->errno (),
								'errmsg'=> $this->StorageObject->errmsg ()
							);
				break;
			case('FileSystem'):
				$fullname=$this->path ."/".  $filename ;
				$result = file_exists ( $fullname);
				return array(	'result'=>  $result ? $fullname : false ,
								'errno'=> $result ? 0 : 9930 ,
								'errmsg'=> $result ? "" : "not find" 
							);
				break;
			default:
				return array(	'result'=>  false ,
								'errno'=> 9999 ,
								'errmsg'=> "unknow type" 
							);
		}
	}

	/*
	string delete (string $filename)
	   删除文件。
	   参数：
	   filename
		 需要删除的文件名
	   返回值：
		 成功返回true，否则false。
				 
	bool delete (string $domain, string $filename)
	*/
	public function delete( $filename ) {
		switch($this->StorageType){
			case('ACE'):
				return array(	'result'=> $this->StorageObject->delete( $filename ) ,
								'errno'=> $this->StorageObject->errnum(),
								'errmsg'=> $this->StorageObject->errmsg()
							);
				break;
			case('SAE'):
				return array(	'result'=> $this->StorageObject->delete($this->path, $path ."/".  $filename ),
								'errno'=> $this->StorageObject->errno (),
								'errmsg'=> $this->StorageObject->errmsg ()
							);
				break;
			case('FileSystem'):
				$fullname=$this->path ."/".  $filename ;
				$result = unlink ( $fullname);
				return array(	'result'=>  $result ,
								'errno'=> $result ? 0 : 9930 ,
								'errmsg'=> $result ? "" : "not find" 
							);
				break;
			default:
				return array(	'result'=>  false ,
								'errno'=> 9999 ,
								'errmsg'=> "unknow type" 
							);
		}
	}

	/*
	string fileExists (string $filename)
	   判断文件是否存在。
	   参数：
	   filename
		  需要判断的文件名
	   返回值：
		  存在返回true，否则false。
				 
	bool fileExists (string $domain, string $filename)
	*/
	public function fileExists( $filename ) {
		switch($this->StorageType){
			case('ACE'):
				return array(	'result'=> $this->StorageObject->fileExists( $filename ) ,
								'errno'=> $this->StorageObject->errnum(),
								'errmsg'=> $this->StorageObject->errmsg()
							);
				break;
			case('SAE'):
				return array(	'result'=> $this->StorageObject->fileExists($this->path, $path ."/".  $filename ),
								'errno'=> $this->StorageObject->errno (),
								'errmsg'=> $this->StorageObject->errmsg ()
							);
				break;
			case('FileSystem'):
				$fullname=$this->path ."/".  $filename ;
				$result = file_exists ( $fullname);
				return array(	'result'=>  $result ,
								'errno'=>  0  ,
								'errmsg'=> "" 
							);
				break;
			default:
				return array(	'result'=>  false ,
								'errno'=> 9999 ,
								'errmsg'=> "unknow type" 
							);
		}
	}

	/*
	array getList ( [string $prefix = ''],[int $num=100], [string $marker = ''], [$delimiter=''])
	   获取文件列表
	   参数： 
	   prefix
			查询的匹配前缀
	   num
		   一次列出的文件个数
	   marker
		  列出文件的起始位置,如a.jpg。
	   delimiter
	   如果再把delimiter设置为  /  时，返回值就只罗列该文件夹下的文件，子文件夹下递归的文件和文件夹不被显示。
	   返回值：
		  成功返回文件列表数组，失败false。
					 
	array getList (string $domain, [string $prefix = '*'], [int $limit = 10], [int $offset = 0])
	*/
	public function getList( $filename ) {
		if( is_array($filename) ){
			switch($this->StorageType){
				case('ACE'):
					return array(	'result'=> call_user_func_array(	$this->StorageObject->getList ,
																		$filename
																	),
									'errno'=> $this->StorageObject->errnum(),
									'errmsg'=> $this->StorageObject->errmsg()
								);
					break;
				case('SAE'):
					$filename['domain']=$this->path;
					return array(	'result'=> call_user_func_array(	$this->StorageObject->getList ,
																		$filename
																	),
									'errno'=> $this->StorageObject->errno (),
									'errmsg'=> $this->StorageObject->errmsg ()
								);
					break;
				case('FileSystem'):
					$result = $this->FileSystemList ();
					return array(	'result'=>  $result ,
									'errno'=>  0  ,
									'errmsg'=> "" 
								);
					break;
				default:
					return array(	'result'=>  false ,
									'errno'=> 9999 ,
									'errmsg'=> "unknow type" 
								);
			}
		}else{
			switch($this->StorageType){
				case('ACE'):
					return array(	'result'=> $this->StorageObject->getList( ) ,
									'errno'=> $this->StorageObject->errnum(),
									'errmsg'=> $this->StorageObject->errmsg()
								);
					break;
				case('SAE'):
					return array(	'result'=> $this->StorageObject->getList($this->path ),
									'errno'=> $this->StorageObject->errno (),
									'errmsg'=> $this->StorageObject->errmsg ()
								);
					break;
				case('FileSystem'):
					$result = $this->FileSystemList ();
					return array(	'result'=>  $result ,
									'errno'=>  0  ,
									'errmsg'=> "" 
								);
					break;
				default:
					return array(	'result'=>  false ,
									'errno'=> 9999 ,
									'errmsg'=> "unknow type" 
								);
			}
		}
	}

}
?>